package ch.reevolt.charger.service;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.IBinder;
import android.os.Vibrator;
import android.widget.Toast;
import ch.reevolt.charger.SafeChargerActivity;

public class ChargerService extends Service {

	private List<IChargerServiceListener> listeners = new ArrayList<IChargerServiceListener>();
	private static ChargerService service;

	private String ACTION_DISCONNECTED = Intent.ACTION_POWER_DISCONNECTED;
	private String ACTION_CONNECTED = Intent.ACTION_POWER_CONNECTED;

	private BroadcastReceiver mIntentReceiver;
	private IntentFilter intentToReceiveFilter;

	private ToneGenerator tg;

	public void startSafety() {
		this.registerReceiver(mIntentReceiver, intentToReceiveFilter);
		Toast.makeText(getApplicationContext(), "registered",
				Toast.LENGTH_SHORT);
	}

	public void stopSafety() {
		this.unregisterReceiver(mIntentReceiver);
	}

	public void startAlert() {
		Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		v.vibrate(300);

	/*	Intent toLaunch = new Intent(getApplicationContext(),
				SafeChargerActivity.class);
		toLaunch.setAction("android.intent.action.MAIN");
		toLaunch.addCategory("android.intent.category.LAUNCHER");*/

	//	sendBroadcast(toLaunch);

		tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
		// tg.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD);
	}

	public void stopAlert() {
		tg.stopTone();
	}

	// Ajout d'un listener
	public void addListener(IChargerServiceListener listener) {
		if (listeners != null)
			listeners.add(listener);

	}

	// Suppression d'un listener
	public void removeListener(IChargerServiceListener listener) {
		if (listeners != null) {
			listeners.remove(listener);
		}
	}

	// Notification des listeners
	private void fireDataChanged(Object data) {
		if (listeners != null) {
			for (IChargerServiceListener listener : listeners) {
				listener.dataChanged(data);
			}
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		service = this;

		mIntentReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (action == ACTION_CONNECTED) {
					stopAlert();
				} else if (action == ACTION_DISCONNECTED) {
					startAlert();

					Intent dialogIntent = new Intent(getBaseContext(),
							SafeChargerActivity.class);
					dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getApplication().startActivity(dialogIntent);

				}
			}
		};

		intentToReceiveFilter = new IntentFilter();
		intentToReceiveFilter.addAction(ACTION_CONNECTED);
		intentToReceiveFilter.addAction(ACTION_DISCONNECTED);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public static ChargerService getService() {
		if (service != null)
			return service;
		else {
			return null;
		}
	}

}